package com.ht.medicaltreatment.activity.haocai

import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.view.forEach
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.bean.BuildProjectConsumable
import com.ht.medicaltreatment.http.api.BuildProjectConsumableAPI
import com.ht.medicaltreatment.viewmodel.CreateZTViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.btnPositive
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etFormName
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etGName
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etProjectCode
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etSampleName
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etUser
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.imgAdd
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.llContent
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.scroll

/**
 * 新建专题
 */
class CreateZhuanTiActivity:BaseActivity(), OnItemChildClickListener {

    override fun getLayoutId() = R.layout.activity_create_zhuanti_layout

    private var code = 1
    private val model by viewModels<CreateZTViewModel>()
    private var isCheckNumber = false

    override fun init() {
        setTitle("新建专题")
        //添加
        imgAdd.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.item_create_zhuanti_layout,null,false)
            val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
            params.topMargin = 20
            params.leftMargin = 10
            params.rightMargin = 10
            view.layoutParams = params
            val tvCode = view.findViewById<TextView>(R.id.tvCode)
            tvCode.setText("$code")
            llContent.addView(view)
            scroll.post {
                scroll.fullScroll(View.FOCUS_DOWN)
            }
            code++
        }

        btnPositive.setOnClickListener {
            showProgress()
            submit()
        }

        //校验专题号
        etSampleName.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus)return@setOnFocusChangeListener
            model.checkNumber("", "0", etSampleName.text.toString()) {
                isCheckNumber = it
                if (!it) {
                    showToast("该专题号已存在，请修改")
                }
            }
        }
    }

    fun submit(){
        if(!isCheckNumber){
            showToast("请修改专题号")
            return
        }
        showProgress()
        //主键id  修改的要传，不修改不传

        //药物名称;供试品名称
        val drugName = etGName.text.toString()
        //项目名称  专题名称
        val name = etFormName.text.toString()
        //项目编号
        val projectNumber = etProjectCode.text.toString()
        //专题号
        val protocolNumber  = etSampleName.text.toString()
        //负责人/主要研究者;专题负责人
        val responsiblePerson   = etUser.text.toString()
        val list = arrayListOf<BuildProjectConsumable>()
        llContent.forEach {
            val etName = it.findViewById<EditText>(R.id.etName)
            val etRongLiang = it.findViewById<EditText>(R.id.etRongLiang)
            val etCount = it.findViewById<EditText>(R.id.etCount)
            list += BuildProjectConsumable(etName.text.toString(),etCount.text.toString(),etRongLiang.text.toString())
        }
       val api =  BuildProjectConsumableAPI(
            drugName = drugName,
            name = name,
            projectNumber = projectNumber,
            protocolNumber = protocolNumber,
            responsiblePerson = responsiblePerson,
            projectConsumableList = list)

        model.submit(api){
            showToast("创建成功")
            finish()
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

    }
}